package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.config.annotation.RequiresPermissions;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.entity.EduSubject;
import com.zsr.example.service.EduSubjectService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.constants.ErrorEnum;
import com.zsr.example.vo.SubjectNestedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.zsr.example.util.constants.SysLogConstants.DELETE;
import static com.zsr.example.util.constants.SysLogConstants.UPLOAD;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-11-13
 */
@RestController
@RequestMapping("/subject")
public class EduSubjectController {

    @Autowired
    private EduSubjectService subjectService;

    /**
     * 添加课程分类
     *
     * @param file
     * @return
     */
    @OptLog(optType = UPLOAD,optDesc = "上传分类表格")
    @RequiresPermissions("subjectExcel:upload")
    @PostMapping("addSubjectByExcel")
    @CacheEvict(value = "eduSubject", allEntries = true,cacheManager="redisCacheManager")//清空
    public JSONObject addSubjectByExcel(MultipartFile file) {
        subjectService.saveSubject(file, subjectService);
        return CommonUtil.success();
    }

    /**
     * 课程分类列表[树形]
     *
     * @return
     */
    @RequiresPermissions("subject:list")
    @GetMapping("getSubject")
    public JSONObject getSubject() {
        List<SubjectNestedVo> list = subjectService.getAllSubject();
        return CommonUtil.successJson(list);
    }

    @RequiresPermissions("subject:add")
    @PostMapping("addSubject")
    public JSONObject addSubject(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "title");
        String id = requestJson.getString("id");
        String title = requestJson.getString("title");
        //检查数据库是否有重复
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",title);
        EduSubject currentSubject = subjectService.getOne(wrapper);
        if(currentSubject!=null)throw  new CommonJsonException(ErrorEnum.E_80002);

        EduSubject eduSubject = new EduSubject()
                .setTitle(title).setParentId("0");
        if (!StringUtils.isEmpty(id)) {//新增二级分类
            eduSubject.setParentId(id);
        }
        boolean result = subjectService.save(eduSubject);
        return result ? CommonUtil.successJson() : CommonUtil.errorJson();

    }

    @RequiresPermissions("subject:update")
    @PostMapping("updateSubject")
    public JSONObject updateSubject(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "id,title");
        String id = requestJson.getString("id");
        String title = requestJson.getString("title");
        EduSubject eduSubject = new EduSubject()
                .setId(id).setTitle(title);
        boolean result = subjectService.updateById(eduSubject);
        return result ? CommonUtil.successJson() : CommonUtil.errorJson();

    }

    @RequiresPermissions("subject:delete")
    @GetMapping("deleteSubject/{id}")
    public JSONObject deleteSubject(@PathVariable String id) {
        if(StringUtils.isEmpty(id))throw new CommonJsonException(ErrorEnum.E_90003);
        //如果是一级分类 下面有子类型不可删除
        EduSubject currentSubject = subjectService.getById(id);
        if (currentSubject.getParentId().equals("0")) {
            QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id", id);
            EduSubject subjectTwo = subjectService.getOne(wrapper);
            if (subjectTwo != null) throw new CommonJsonException(ErrorEnum.E_80001);
            //TODO 查看课程有没有分类
        }else{
            //TODO 查看课程有没有分类
        }
        boolean result = subjectService.removeById(id);
        return result ? CommonUtil.successJson() : CommonUtil.errorJson();

    }
}

